//
//  ViewController.m
//  TUTKTest
//
//  Created by kai_leedarson on 2017/10/23.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "ViewController.h"
#import "Client.h"
#import "AAPLEAGLLayer.h"


// 从 视频  到 打印buffer 的全流程
@interface ViewController ()
{
    AAPLEAGLLayer *playLayer;
}
@property (nonatomic, assign) UInt32            timeStamp;          //时间计时
@property (nonatomic, weak  ) NSTimer           *stampTimer;        //无音频时间戳计时器
@property (nonatomic, strong) NSLock            *lock;              //异步线程下的锁
@property (nonatomic, strong) NSMutableArray    *pixelBufferArray;  //pixel队列
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pixelBufferArray = [[NSMutableArray alloc] init];
    self.lock = [[NSLock alloc] init];
    self.timeStamp = 0;

    // Do any additional setup after loading the view, typically from a nib.
    Client *client = [[Client alloc] init];
    //    [client start:@"K0000000000000000001"]; // Put your device's UID here.
    [client start:@"FTKUA97WZXL4TMPGUHE1" andDelegate:(id<VideoDecoderDelegate>)self];
    
    playLayer = [[AAPLEAGLLayer alloc] initWithFrame:self.view.bounds];
    playLayer.backgroundColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:playLayer];
    
}


#pragma mark - VideoDecoderDelegate
- (void)decoderPixelBuffer:(CVPixelBufferRef)pixelBuffer timeStamp:(UInt32)timeStamp {
    
    if (pixelBuffer) {
        if (playLayer) {
            playLayer.pixelBuffer = pixelBuffer;
            CVPixelBufferRelease(pixelBuffer);
        }
    }

    
    //首次接收视频，开启时间轴
//    dispatch_async(dispatch_get_main_queue(), ^{
//        if (self.timeStamp == 0 && self.stampTimer == nil) {
//            self.timeStamp = timeStamp;
//            self.stampTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(stampTimerAction:) userInfo:nil repeats:YES];
//        }
//        if (self.timeStamp > timeStamp) {
//            self.timeStamp = timeStamp;
//        }
//    });
//
//    PixelBufferData *pixelBufferData = [[PixelBufferData alloc] initWithPixelBuffer:pixelBuffer timeStamp:timeStamp];
//    [self.lock lock];
//    [self.pixelBufferArray addObject:pixelBufferData];
//    [self.lock unlock];
//
//    //长时间没有音频，开启时间轴
//    if (self.pixelBufferArray.count > 30) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            if (self.stampTimer == nil) {
//                self.timeStamp = timeStamp;
//                self.stampTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(stampTimerAction:) userInfo:nil repeats:YES];
//            }
//            if (self.timeStamp > timeStamp) {
//                self.timeStamp = timeStamp;
//            }
//        });
//    }
//    if(pixelBuffer)
//    {
//        playLayer.pixelBuffer = pixelBuffer;
//        CVPixelBufferRelease(pixelBuffer);
//    }
}

- (void)stampTimerAction:(NSTimer *)timer {
    [self.lock lock];
    while (TRUE) {
        PixelBufferData *pixelBufferData = [self.pixelBufferArray firstObject];
        if (pixelBufferData && pixelBufferData.timeStamp<=self.timeStamp) {
            if (playLayer) {
                playLayer.pixelBuffer = pixelBufferData.pixelBuffer;
            }
            [self.pixelBufferArray removeObjectAtIndex:0];
        } else {
            break;
        }
    }
    [self.lock unlock];
    
    self.timeStamp = self.timeStamp + 10;
}

@end

@implementation PixelBufferData

- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer timeStamp:(UInt32)timeStamp
{
    self = [super init];
    if (self) {
        self.pixelBuffer = pixelBuffer;
        self.timeStamp = timeStamp;
    }
    return self;
}

- (void)dealloc
{
    CVPixelBufferRelease(self.pixelBuffer);
}

@end
